package com.tt.fashionWebSite.dao.jpa;

import com.tt.fashionWebSite.dao.HoaDonDao;
import com.tt.fashionWebSite.domain.DatHang;
import com.tt.fashionWebSite.domain.HoaDon;
import com.tt.fashionWebSite.domain.HoaDonFull;
import com.tt.fashionWebSite.domain.Luat;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("hoaDon")
public class HoaDonJpaDao extends GenericJpaDao<HoaDon, String> implements HoaDonDao {
    public HoaDonJpaDao(){
        super(HoaDon.class);
    }

    @Override
    public List<HoaDonFull> getBillCommon(){
//        return getEntityManager().createNamedQuery(HoaDon.GET_BILL_ALL, HoaDon.class).getResultList();
        String sql = "SELECT c FROM HoaDonFull c  ORDER BY c.ngayLapHD DESC";
        return getEntityManager().createQuery(sql, HoaDonFull.class).getResultList();
    }

    @Override
    public List<HoaDon> getDataId(String id){
//        return getEntityManager().createNamedQuery(Luat.GET_INFO_LAWREASON, Luat.class).getResultList();
        return getEntityManager().createNamedQuery(HoaDon.GET_BILL_ALL_ID, HoaDon.class)
                .setParameter("Id", id)
                .getResultList();
    }

    @Override
    public List<HoaDon> updateStatusBillCommon(String maHD, String finish, String status){
        String sql = "UPDATE HoaDon c SET c.thanhToan = "+ finish +", c.trangThai = "+ status +" WHERE c.maHD = '"+ maHD+"'";
//        getEntityManager().createQuery(sql, HoaDon.class);
//        String sql1 = "SELECT c FROM HoaDon c";
        return getEntityManager().createQuery(sql, HoaDon.class).getResultList();
    }

    @Override
    public List<HoaDonFull> getDataSortInBill(String name, String updown){
        String sql = "SELECT c FROM HoaDonFull c  ORDER BY c." +name+ " "+updown;
        return getEntityManager().createQuery(sql, HoaDonFull.class).getResultList();
//        return getEntityManager().createNamedQuery(HoaDon.GET_BILL_ALL_TEST, HoaDon.class)
//                .getResultList();
    }

    @Override
    public List<HoaDonFull> getDataSearchInBill(String name, String content){
        String sql = "SELECT c FROM HoaDonFull c  WHERE c." +name+ " like '%"+content+"%'";
        return getEntityManager().createQuery(sql, HoaDonFull.class).getResultList();
    }

    @Override
    public List<HoaDonFull> getDateSearchInBill(String name, String dateBegin, String dateEnd){
        String sql = "SELECT c FROM HoaDonFull c  WHERE c." +name+ " BETWEEN '"+dateBegin+"' AND '"+dateEnd+"'";
        return getEntityManager().createQuery(sql, HoaDonFull.class).getResultList();
    }

    @Override
    public List<HoaDonFull> getFinishTimeSearchInBill(String name, String finish, String time, String dateBegin, String dateEnd){
        String sql = "SELECT c FROM HoaDonFull c  WHERE c." +name+ " = "+finish+" AND c."+time+" BETWEEN '"+dateBegin+"' AND '"+dateEnd+"'";
        return getEntityManager().createQuery(sql, HoaDonFull.class).getResultList();
    }

    @Override
    public List<HoaDonFull> getStatusTimeSearchInBill(String name, String status, String time, String dateBegin, String dateEnd){
        String sql = "SELECT c FROM HoaDonFull c  WHERE c." +name+ " = "+status+" AND c."+time+" BETWEEN '"+dateBegin+"' AND '"+dateEnd+"'";
        return getEntityManager().createQuery(sql, HoaDonFull.class).getResultList();
    }

    @Override
    public List<HoaDonFull> getFinish_StatusTimeSearchInBill(String finish, String status, String time, String dateBegin, String dateEnd){
        String sql = "SELECT c FROM HoaDonFull c  WHERE c.thanhToan = " +finish+ " AND c.trangThai = "+status+" AND c."+time+" BETWEEN '"+dateBegin+"' AND '"+dateEnd+"'";
        return getEntityManager().createQuery(sql, HoaDonFull.class).getResultList();
    }

    @Override
    public List<HoaDonFull> getFinishSearchInBill(String name, String finish){
        String sql = "SELECT c FROM HoaDonFull c  WHERE c." +name+ " = "+finish;
        return getEntityManager().createQuery(sql, HoaDonFull.class).getResultList();
//        return getEntityManager().createNamedQuery(HoaDon.GET_BILL_ALL_TEST, HoaDon.class)
//                .getResultList();
    }

    @Override
    public List<HoaDonFull> getStatusSearchInBill(String name, String status){
        String sql = "SELECT c FROM HoaDonFull c  WHERE c." +name+ " = "+status;
        return getEntityManager().createQuery(sql, HoaDonFull.class).getResultList();
    }

    @Override
    public List<HoaDonFull> getFinish_StatusSearchInBill(String finish, String status){
        String sql = "SELECT c FROM HoaDonFull c  WHERE c.trangThai = "+status+" AND c.thanhToan = "+finish+"";
        return getEntityManager().createQuery(sql, HoaDonFull.class).getResultList();
    }

    @Override
    public List<HoaDon> getAllBillByUser(String userName) {
        return getEntityManager().createNamedQuery(HoaDon.GET_BILL_ALL_USER, HoaDon.class)
              .setParameter("Id", userName)
                .getResultList();
    }
}
